package com.xh.bussiness.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.bean.request.MenuRequest;
import com.xh.bussiness.permission.dao.MenuMapper;
import com.xh.bussiness.permission.model.Menu;
import com.xh.bussiness.permission.service.IMenuService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Override
    public XhPage queryMenuPage(XhPage page, MenuRequest bean) {
        QueryWrapper qw = new QueryWrapper<Menu>();
        qw.select("*");
        //拼装条件
        qw = prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page, qw);
    }


    @Override
    public List queryMenu(MenuRequest bean) {
        QueryWrapper qw = new QueryWrapper<Menu>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }


    @Override
    public String addMenu(MenuRequest bean) {
        Menu menu = new Menu();

        menu.setName(bean.getName());
        menu.setParentId(bean.getParentId());
        menu.setIconUrl(bean.getIconUrl());
        menu.setUrl(bean.getUrl());
        menu.setExtendId(bean.getExtendId());
        menu.setIsShow(bean.getIsShow());
        menu.setIsShowInPermission(bean.getIsShowInPermission());
        menu.setFunctionCodes(bean.getFunctionCodes());
        menu.setOrderNo(bean.getOrder());
        this.insert(menu);

        return null;
    }


    @Override
    public String modifyMenu(MenuRequest bean) {
        Menu menu = this.getById(bean.getId());
        if (menu == null) {
            return "当前菜单id不存在";
        }

        menu.setName(bean.getName());
        menu.setParentId(bean.getParentId());
        menu.setIconUrl(bean.getIconUrl());
        menu.setUrl(bean.getUrl());
        menu.setExtendId(bean.getExtendId());
        menu.setIsShow(bean.getIsShow());
        menu.setIsShowInPermission(bean.getIsShowInPermission());
        menu.setFunctionCodes(bean.getFunctionCodes());
        menu.setOrderNo(bean.getOrder());
        this.update(menu);

        return null;
    }


    @Override
    public String deleteMenu(String id) {
        Menu menu = this.getById(id);
        if (menu == null) {
            return "该菜单id不存在";
        }
        menu.deleteById(id);

        return null;
    }

    @Override
    public List<Menu> getChild(String id) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("parent_id", id);
        return this.dao.selectList(qw);
    }

    @Override
    public List<Menu> queryMenuTree() {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.orderByAsc("order_no");
        List<Menu> all = super.queryByCondition(qw);
        List<Menu> roots = getRoots(all);
        creatTree(roots, all);
        return roots;
    }

    private List<Menu> getRoots(List<Menu> all) {
        List<Menu> roots = new ArrayList<Menu>();
        all.forEach(b -> {
            if (StringUtils.isEmpty(b.getParentId())) {
                roots.add(b);
            }
        });
        return roots;
    }

    private void creatTree(List<Menu> roots, List<Menu> all) {
        for (Menu root : roots) {
            List<Menu> childs = new ArrayList<>();
            for (Menu chapter : all) {
                if (chapter.getParentId() != null && chapter.getParentId().equals(root.getId())) {
                    childs.add(chapter);
                }
            }
            root.setChilds(childs);
            creatTree(childs, all);
        }
    }

    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(MenuRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        return qw;
    }
}
